package chain_user_config

import (
	"chain_bridge_service/internal/code"
	"chain_bridge_service/internal/pkg/core"
	"chain_bridge_service/internal/pkg/validation"
	"chain_bridge_service/internal/repository/mongodb/user"
	"chain_bridge_service/internal/services/chain_user_config"
	"chain_bridge_service/internal/tools"
	"encoding/json"
	"net/http"
)

type updateRequest struct {
	Id             string `uri:"id" json:"id"`
	CollectAddress string `bson:"collectAddress" json:"collectAddress"`
	FeeAddress     string `bson:"feeAddress" json:"feeAddress"`
	ChainConfigId  string `bson:"chainConfigId" json:"chainConfigId"`
	ChainName      string `bson:"chainName" json:"chainName"`
}

type updateResponse struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

type updateAPIRequest struct {
	Id string `uri:"id"` // HashID
}

func (h *handler) Update() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(updateRequest)
		res := new(updateResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		updateId := new(updateAPIRequest)
		if err := ctx.ShouldBindURI(updateId); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}
		userInfo := ctx.GetInfo("userInfo").(*user.User)
		updateChainData := new(chain_user_config.UpdateChainData)
		updateChainData.UserId = userInfo.Id
		updateChainData.Id = updateId.Id
		updateChainData.CollectAddress = req.CollectAddress
		//updateChainData.FeeAddress = req.FeeAddress
		updateChainData.ChainConfigId = req.ChainConfigId
		result := h.chainUserConfigService.Update(ctx, updateChainData)
		res.Code = code.StatusOK
		res.Data = result
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}

func (h *handler) UpdateAddress() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(updateRequest)
		res := new(updateResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusBadRequest,
				code.ParamBindError,
				validation.Error(err)).WithError(err),
			)
			return
		}

		var flag string
		if req.ChainName == "TRX" {
			flag = "TRX"
		} else {
			flag = "EVM"
		}

		url := "http://192.168.0.188:8888/api/generateAddress?flag=" + flag
		body, err := tools.SendGet(url)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.SendGetError,
				code.Text(code.SendGetError)).WithError(err),
			)
			return
		}
		var data AddressData
		err = json.Unmarshal(body, &data)
		if err != nil {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.JsonError,
				code.Text(code.JsonError)).WithError(err),
			)
			return
		}
		if data.Code != 200 {
			ctx.AbortWithError(core.Error(
				http.StatusOK,
				code.CodeError,
				code.Text(code.CodeError)).WithError(err),
			)
			return
		}

		updateChainData := new(chain_user_config.UpdateChainData)
		updateChainData.Id = req.Id
		updateChainData.FeeAddress = data.Data.Address
		updateChainData.BusinessId = data.Data.Id

		result := h.chainUserConfigService.UpdateAddress(ctx, updateChainData)
		res.Code = code.StatusOK
		res.Data = result
		res.Msg = code.Text(code.StatusOK)
		ctx.Payload(res)
	}
}
